using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class ChatSlideImgPage : BasePage
	{
		private bool bIsFirstLoad = true;
		private ChatMsg mPageinMsg;
		private ApplicationBar mBar;
		private ApplicationBarIconButton SavaMenuBtn;
		private ApplicationBarMenuItem DownLoadItem;
		private ApplicationBarIconButton PreviousIcon;
		private ApplicationBarIconButton NextIcon;
		internal Grid LayoutRoot;
		internal ChatImgPanoSlideContainer mPanoramaSlideCtrl;
		private bool _contentLoaded;
		public ChatSlideImgPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.ChatSlideImgPage_Loaded));
		}
		private void ChatSlideImgPage_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.bIsFirstLoad)
			{
				base.set_ApplicationBar(this.GetAppBar());
				this.setAppBarStyle(base.get_ApplicationBar());
				if (this.mPanoramaSlideCtrl.ChatSlideImg[1].hdImageLen != 0 && this.mPanoramaSlideCtrl.ChatSlideImg[1].hdImageLen != this.mPanoramaSlideCtrl.ChatSlideImg[1].imageLen && !this.mPanoramaSlideCtrl.ChatSlideImg[1].bDownloadFullImage)
				{
					this.mBar.get_MenuItems().Insert(0, this.DownLoadItem);
				}
				this.bIsFirstLoad = false;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.LoadOtherMsg();
				this.RefreshAppbarBtn();
			});
			this.mPanoramaSlideCtrl.mSlideSelectedChanged += new ChatImgPanoSlideContainer.SlideSelectedChanged(this.mPanoramaSlideCtrl_mSlideSelectedChanged);
		}
		private void mPanoramaSlideCtrl_mSlideSelectedChanged(object sender, object object_0)
		{
			this.RefreshAppbarMenuItem();
			this.RefreshAppbarBtn();
		}
		private void LoadOtherMsg()
		{
			int nowIdx = 0;
			List<ChatMsg> msgList = StorageMgr.chatMsg.getMsgList(this.mPageinMsg.strTalker, 0, StorageMgr.chatMsg.getMsgNum(this.mPageinMsg.strTalker));
			for (int i = msgList.get_Count() - 1; i >= 0; i--)
			{
				if (!msgList.get_Item(i).IsImage())
				{
					msgList.RemoveAt(i);
				}
			}
			for (int j = 0; j < msgList.get_Count(); j++)
			{
				if (msgList.get_Item(j).nMsgLocalID == this.mPageinMsg.nMsgLocalID)
				{
					nowIdx = j;
					IL_86:
					this.mPanoramaSlideCtrl.chatMsgList = msgList;
					this.mPanoramaSlideCtrl.InitData(nowIdx);
					return;
				}
			}
			goto IL_86;
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 == null)
			{
				return;
			}
			if (object_0.Length < 1)
			{
				return;
			}
			this.mPageinMsg = (object_0[0] as ChatMsg);
			if (this.mPageinMsg == null)
			{
				return;
			}
			this.mPanoramaSlideCtrl.PreLoadFirstImg(this.mPageinMsg);
		}
		private ApplicationBar GetAppBar()
		{
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			this.DownLoadItem = new ApplicationBarMenuItem();
			this.DownLoadItem.set_Text(strings.Image_HDImageViewHD);
			this.DownLoadItem.add_Click(new EventHandler(this.DownLoadItem_Click));
			this.PreviousIcon = new ApplicationBarIconButton(new Uri("/Source/Plugin/Sns/Common/Resource/ico_prev.png", 2));
			this.PreviousIcon.set_Text(strings.Album_SlideView_Previous);
			this.PreviousIcon.add_Click(new EventHandler(this.PreviousIcon_Click));
			this.mBar.get_Buttons().Add(this.PreviousIcon);
			this.SavaMenuBtn = new ApplicationBarIconButton(new Uri("/Resource/Img/Chat/Save.png", 2));
			this.SavaMenuBtn.set_Text(strings.ImageViewSave);
			this.SavaMenuBtn.add_Click(new EventHandler(this.SavaMenuBtn_Click));
			this.mBar.get_Buttons().Add(this.SavaMenuBtn);
			if (this.mPanoramaSlideCtrl.bDownloadingImage)
			{
				this.SavaMenuBtn.set_IsEnabled(false);
			}
			this.NextIcon = new ApplicationBarIconButton(new Uri("/Source/Plugin/Sns/Common/Resource/ico_next.png", 2));
			this.NextIcon.set_Text(strings.Album_SlideView_Next);
			this.NextIcon.add_Click(new EventHandler(this.NextIcon_Click));
			this.mBar.get_Buttons().Add(this.NextIcon);
			return this.mBar;
		}
		private void RefreshAppbarMenuItem()
		{
			if (this.mPanoramaSlideCtrl.bHasFullImage && !this.mPanoramaSlideCtrl.bDownloadingHdImage)
			{
				if (this.mBar.get_MenuItems().get_Count() == 0)
				{
					this.mBar.get_MenuItems().Add(this.DownLoadItem);
				}
			}
			else
			{
				if (this.mBar.get_MenuItems().get_Count() != 0)
				{
					this.mBar.get_MenuItems().RemoveAt(0);
					return;
				}
			}
		}
		private void RefreshAppbarBtn()
		{
			if (this.mPanoramaSlideCtrl.nowIndex == 0)
			{
				this.PreviousIcon.set_IsEnabled(false);
			}
			else
			{
				this.PreviousIcon.set_IsEnabled(true);
			}
			if (this.mPanoramaSlideCtrl.nowIndex == this.mPanoramaSlideCtrl.chatMsgList.get_Count() - 1)
			{
				this.NextIcon.set_IsEnabled(false);
				return;
			}
			this.NextIcon.set_IsEnabled(true);
		}
		public void EnableSaveIcon()
		{
			if (this.SavaMenuBtn != null)
			{
				this.SavaMenuBtn.set_IsEnabled(true);
			}
		}
		public void DisAbleSaveIcon()
		{
			if (this.SavaMenuBtn != null)
			{
				this.SavaMenuBtn.set_IsEnabled(false);
			}
		}
		private void PreviousIcon_Click(object sender, EventArgs e)
		{
			this.mPanoramaSlideCtrl.GoPrevious();
		}
		private void NextIcon_Click(object sender, EventArgs e)
		{
			this.mPanoramaSlideCtrl.GoNext();
		}
		private void DownLoadItem_Click(object sender, EventArgs e)
		{
			if (!NetworkDeviceWatcher.isCurrentWifi())
			{
				MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Image_HDImageViewHD, string.Format(strings.Image_HDImageViewAlertFormat, this.mPanoramaSlideCtrl.nHdImageLen), true, strings.COM_Confirm, strings.COM_Cancel);
				mMMessageBox.ReturnEvent = new DlgReturnEvent(this.ViewHDImageDialogReturn);
				return;
			}
			this.DownLoadFullImg();
		}
		private void DownLoadFullImg()
		{
			this.mBar.get_MenuItems().RemoveAt(0);
			this.mPanoramaSlideCtrl.ChatSlideImg[this.mPanoramaSlideCtrl.panoramaCtrl.SelectedIndex].DownLoadFullImgView();
		}
		public void ViewHDImageDialogReturn(DlgReturnEventArg dlgReturnEventArg_0)
		{
			int userClickResult = MMMessageBox.GetUserClickResult(dlgReturnEventArg_0);
			int num = userClickResult;
			if (num != 0)
			{
				return;
			}
			this.DownLoadFullImg();
		}
		private void SavaMenuBtn_Click(object sender, EventArgs e)
		{
			this.SavaPic();
		}
		private void SavaPic()
		{
			if (string.IsNullOrEmpty(this.mPanoramaSlideCtrl.CurrentImgSrc))
			{
				return;
			}
			if (this.mPanoramaSlideCtrl.CurrentImgCtrl.get_Source() == null)
			{
				return;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(this.mPanoramaSlideCtrl.CurrentImgSrc, 3, 1))
					{
						MediaLibrary mediaLibrary = new MediaLibrary();
						mediaLibrary.SavePicture(this.GetExisPicName(), isolatedStorageFileStream);
						isolatedStorageFileStream.Close();
					}
				}
				MessageBoxManager.ShowToast(strings.ImageViewSaveSuccess, 2500, null, false);
			}
			catch (Exception ex)
			{
				Log.smethod_1(base.GetType().get_Name(), "SavaPic Exception" + ex.get_StackTrace());
				MMMessageBox.Show(strings.ImageViewSaveFail, true, "确定");
			}
		}
		private string GetExisPicName()
		{
			return "MicroMsg" + DateTime.get_Now().get_Ticks() + ".jpg";
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/SlideImageView/ChatSlideImgPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.mPanoramaSlideCtrl = (ChatImgPanoSlideContainer)base.FindName("mPanoramaSlideCtrl");
		}
	}
}
